package light.mvc.service.hrs.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import light.mvc.dao.BaseDaoI;
import light.mvc.framework.constant.GlobalConstant;
import light.mvc.model.hrs.Thrsdata;
import light.mvc.model.hrs.Thrsmember;
import light.mvc.pageModel.base.PageFilter;
import light.mvc.pageModel.hrs.HRSData;
import light.mvc.service.hrs.HRSDataServiceI;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class HRSDataServiceImpl implements HRSDataServiceI{

	@Autowired
	private BaseDaoI<Thrsdata> dataDao;
	@Autowired
	private BaseDaoI<Thrsmember> memberDao;

	
	@Override 
	public void delete(Long id,HttpServletRequest request) {
		Thrsdata t = dataDao.get(Thrsdata.class, id);
		dataDao.delete(t); 
		dataDao.deleteFile(t.getFacePhotoPath(),request);
		dataDao.deleteFile(t.getTonguePhotoPath(), request);
	}
	
	@Override
	public HRSData get(Long id) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		Thrsdata t = dataDao.get("from Thrsdata t  where t.id = :id", params);
		HRSData u = new HRSData();
		BeanUtils.copyProperties(t, u);
		if (t.getUploaderType()!=null&&t.getUploaderType().length()!=0&&t.getUploaderId()!=0) {
//			如果上传的用户类型是普通用户
			if (t.getUploaderType().equals(GlobalConstant.MEMBER)) {
				Thrsmember m = memberDao.get("from Thrsmember m  where m.id = "+t.getUploaderId());
				u.setUploaderLoginName(m.getLoginName());
			}
		}
		return u;
	}

	@Override
	public List<HRSData> dataGrid(HRSData data, PageFilter ph) {
		List<HRSData> ul = new ArrayList<HRSData>();
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = " from Thrsdata t ";
		List<Thrsdata> l = dataDao.find(hql + whereHql(data, params) + orderHql(ph), params, 
				ph.getPage(), ph.getRows());
		for (Thrsdata t : l) {
			HRSData u = new HRSData();
			BeanUtils.copyProperties(t, u);
			if (t.getUploaderType()!=null&&t.getUploaderType().length()!=0&&t.getUploaderId()!=0) {
				if (t.getUploaderType().equals(GlobalConstant.MEMBER)) {
					Thrsmember m = memberDao.get("from Thrsmember m  where m.id = "+t.getUploaderId());
					u.setUploaderLoginName(m.getLoginName());
				}
			}
			ul.add(u);
		}
		return ul;
	}

	@Override
	public Long count(HRSData data, PageFilter ph) {
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = " from Thrsdata t ";
		return dataDao.count("select count(*) " + hql + whereHql(data, params), params);
	}

	private String whereHql(HRSData data, Map<String, Object> params) {
		String hql = "";
		if (data != null) {
			hql += " where 1=1 ";
			if (data.getUploaderId()!=0&&data.getUploaderType()!=null&&data.getUploaderType().length()!=0) {
				hql += " and t.uploaderId = "+data.getUploaderId();
				hql += " and t.uploaderType = "+data.getUploaderType();
			}
		}
		return hql;
	}

	private String orderHql(PageFilter ph) {
		String orderString = "";
		if ((ph.getSort() != null) && (ph.getOrder() != null)) {
			orderString = " order by t." + ph.getSort() + " " + ph.getOrder();
		}
		return orderString;
	}
	
	@Override
	public void add(HRSData data){
		Thrsdata d = new Thrsdata();
		BeanUtils.copyProperties(data, d);
		dataDao.save(d);
	}

	@Override
	public HRSData expertGetOne() {
		Thrsdata l = dataDao.get("from  Thrsdata t where t.judgeTime=(select min(judgeTime) from Thrsdata)");
		System.out.println(l.getFacePhotoPath()+" "+l.getJudgeTime());
		HRSData u = new HRSData();
		BeanUtils.copyProperties(l, u);
		return u;
	}

	@Override
	public void dataIncrease(long id) {
		dataDao.executeHql("update Thrsdata t set t.judgeTime=t.judgeTime+1 where t.id="+id);
	}

}
